package cn.edu.ctbu.dp01.service;

import cn.edu.ctbu.dp01.dao.TeacherRepository;
import cn.edu.ctbu.dp01.entity.teacher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class TeacherService {
    @Autowired
    public TeacherRepository teacherRepository;

    /*
     * 获取所有教师
     * @return List<teacher>
     */
    public List<teacher> getAll() {
        return teacherRepository.findAll();
    }

    /*
     * 根据ID查询教师
     * @param id 教师ID 主键，整数
     * @return teacher
     */
    public teacher getById(Integer id) {
        return teacherRepository.findById(id).orElse(null);
    }

    /*
     * 根据姓名查询教师
     * @param name 教师姓名 字符串
     * @return List<teacher>
     */
    public List<teacher> findByName(String name) {
        return teacherRepository.findByNameLike(name);
    }

    /*
     * 添加教师
     * @param teacher 教师对象
     * @return teacher
     */
    public teacher add(teacher teacher) {
        return teacherRepository.save(teacher);
    }

    /*
     * 更新教师信息
     * @param teacher 教师对象
     * @return teacher
     */
    public teacher update(teacher teacher) {
        return teacherRepository.save(teacher);
    }

    /*
     * 删除教师
     * @param id 教师ID 主键，整数
     */
    @Transactional
    public void delete(Integer id) {
        teacherRepository.deleteById(id);
    }
}